package com.hz.house.service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.hz.house.bean.entity.ArticleDetailBean;
import com.hz.house.bean.entity.FAQBean;
import com.hz.house.bean.entity.HotQaBean;
import com.hz.house.bean.entity.OfficialAccountbean;
import com.hz.house.bean.entity.SellManDynamic;

/**
 * 社区部分的缓存服务处理
 * @author lfw
 */
@Service
public class BbsCacheService {
	
	@Autowired
	private RedisTemplate<String,Object> redis;
	
	
	public static final String BBS_HOME_HOTQA_THREE = "bbs_home_hotaq_3";
	public static final String BBS_HOME_HOTQA_DETAIL = "bbs_home_hotaq_detail:";
	public static final String BBS_HOTQA_LIST_PAGE = "bbs_hotqa_list_page";
	public static final String OFFICIAL_USER_ACCOUNT_CACHE= "official_account_cahce";
	public static final String HOUSE_NEWS = "bbs_house_news_list_page"; // 房产新闻
	public static final String ALONE_REPORT = "bbs_alone_report_list_page"; // 独家报道
	public static final String MARKET_POLICY = "bbs_market_policy_list_page"; // 楼市政策
	public static final String LAND_MARKET = "bbs_land_market_list_page"; // 土地市场
	public static final String MANUSCRIPT = "bbs_manuscript_list_page"; // 楼盘通稿/资讯
	public static final String HOUSE_ARTICLE = "bbs_article_detail";
	public static final String SELLMAN_DYNAMIC = "bbs_sellman_dynamic_list_page";
	
	public static final String FAQ_MAXID = "bbs_faq_maxid";
	public static final String BBS_FAQ_PAGE_LIST="bbs_faq_page_list";
	
	@Autowired
	private CommonService commonService;
	
	/**缓存5分钟*/
	public void setBbsHomeHotQa(List<HotQaBean> qas) {
		this.redis.opsForValue().set(BBS_HOME_HOTQA_THREE, qas,5, TimeUnit.MINUTES);
	}
	
	/**获取缓存的首页三个热问*/
	@SuppressWarnings("unchecked")
	public List<HotQaBean> getBbsHomeHotQa(){
		return (List<HotQaBean>) this.redis.opsForValue().get(BBS_HOME_HOTQA_THREE);
	}
	
	/**删除社区首页的三个今日热问的缓存*/
	public void deleteBbsHomeHotQa() {
		this.redis.delete(BBS_HOME_HOTQA_THREE);
	}
	
	/**
	 *	 缓存热问的详情内容
	 * @param qa
	 */
	public void setHotQaDetailCache(HotQaBean qa) {
		this.redis.opsForValue().set(BBS_HOME_HOTQA_DETAIL+qa.getId(), qa,5,TimeUnit.MINUTES);
	}
	
	/**根据id获取缓存的今日热问详情数据*/
	public HotQaBean getHotQaDetail(int qaId) {
		return (HotQaBean) this.redis.opsForValue().get(BBS_HOME_HOTQA_DETAIL+qaId);
	}
	
	/**删除热问的详情缓存*/
	public void deleteHotQaDetail(int qaId) {
		this.redis.delete(BBS_HOME_HOTQA_DETAIL+qaId);
	}
	
	
	//------------全部热问列表缓存 hash------------
	/**缓存今日热问列表当页数据，在后台数据有变动的时候删除*/
	public void setHotQaPageCache(int page,List<HotQaBean> list) {
		this.redis.opsForHash().put(BBS_HOTQA_LIST_PAGE,page+"",list);
	}
	
	/**设置总数*/
	public void setHotQaTotals(int total) {
		this.redis.opsForHash().put(BBS_HOTQA_LIST_PAGE, "total", total);
		this.redis.expire(BBS_HOTQA_LIST_PAGE,10, TimeUnit.MINUTES);
	}
	
	/**获取分页的总条数*/
	public Integer getHotQaTotals() {
		return (Integer) this.redis.opsForHash().get(BBS_HOTQA_LIST_PAGE, "total");
	}
	
	@SuppressWarnings("unchecked")
	/**获取今日热问列表缓存本页数据*/
	public List<HotQaBean> getHotQaListCache(int page){
		return (List<HotQaBean>) this.redis.opsForHash().get(BBS_HOTQA_LIST_PAGE, page+"");
	}
	
	/**删除今日热问列表缓存*/
	public void deleteHotQaListCache() {
		this.redis.delete(BBS_HOTQA_LIST_PAGE);
	}
	
	/**设置官方的账户缓存*/
	public void setOfficicalAccountCache(List<OfficialAccountbean> ofs) {
		this.redis.opsForValue().set(OFFICIAL_USER_ACCOUNT_CACHE, ofs,25,TimeUnit.HOURS);
	}
	
	/**获取官方的账号信息*/
	@SuppressWarnings("unchecked")
	public List<OfficialAccountbean> getOfficialAccount(){
		List<OfficialAccountbean> x = (List<OfficialAccountbean>)
				this.redis.opsForValue().get(OFFICIAL_USER_ACCOUNT_CACHE);
		if(x==null) 
			x = commonService.initOfficialAccount();
		
		return x;
	}
	
	/**删除官方账号的缓存*/
	public void deleteOfficialAccountCache() {
		this.redis.delete(OFFICIAL_USER_ACCOUNT_CACHE);
	}

	/**获取买房必读文章列表*/
	@SuppressWarnings("unchecked")
	public List<ArticleDetailBean> getArticleList(String key, Integer page) {
		return (List<ArticleDetailBean>) redis.opsForHash().get(key,page + "");
	}

	/**设置买房必读文章缓存*/
	public void setArticlePageCache(Integer page, String key, List<ArticleDetailBean> arts) {
		redis.opsForHash().put(key,page + "",arts);
	}

	/**获取买房必读对应文章总条数*/
	public Integer getArticleTotal(String key) {
		return (Integer) redis.opsForHash().get(key,"total");
	}

	/**设置买房必读对应文章总条数*/
	public void setArticleTotal(String key, Integer total) {
		redis.opsForHash().put(key,"total",total);
	}

	/**查询文章详情*/
	public ArticleDetailBean getArticleDetail(Integer id) {
		return (ArticleDetailBean) redis.opsForHash().get(HOUSE_ARTICLE ,id + "");
	}

	/**添加文章详情*/
	public void setArticleDetail(ArticleDetailBean at) {
		redis.opsForHash().put(HOUSE_ARTICLE,at.getId() + "",at);
		redis.expire(HOUSE_ARTICLE,1,TimeUnit.HOURS);
	}

	/**社区置业顾问动态列表*/
	@SuppressWarnings("unchecked")
	public List<SellManDynamic> getSellManDyns(Integer page) {
		return (List<SellManDynamic>) redis.opsForHash().get(SELLMAN_DYNAMIC,page + "");
	}

	/**设置社区置业顾问动态列表数据*/
	public void setSellManDyns(Integer page,List<SellManDynamic> dyns) {
		redis.opsForHash().put(SELLMAN_DYNAMIC,page + "",dyns);
	}

	/**查询社区置业顾问动态列表总数*/
	public Integer getDynamicTotal() {
		return (Integer) redis.opsForHash().get(SELLMAN_DYNAMIC,"total");
	}

	/**设置社区置业顾问列表总条数*/
	public void setDynamicTotal(Integer total) {
		redis.opsForHash().put(SELLMAN_DYNAMIC,"total",total);
	}

	/**查询楼盘资讯列表*/
	@SuppressWarnings("unchecked")
	public List<ArticleDetailBean> getManuscriptList(Integer page) {
		return (List<ArticleDetailBean>) redis.opsForHash().get(MANUSCRIPT,page + "");
	}

	/**设置楼盘资讯列表*/
	public void setManuscriptList(Integer page, List<ArticleDetailBean> arts) {
		redis.opsForHash().put(MANUSCRIPT,page + "",arts);
	}

	/**获取楼盘资讯总条数*/
	public Integer getManuscriptTotal() {
		return (Integer) redis.opsForHash().get(MANUSCRIPT,"total");
	}

	/**设置楼盘资讯总条数*/
	public void setManuscriptTotal(Integer total) {
		redis.opsForHash().put(MANUSCRIPT,"total",total);
	}
 
	/**项目启动设置最大id*/
	public void setMaxFaqid(int maxId) {
		this.redis.opsForValue().set(FAQ_MAXID, maxId);
	}
	
	private String faqIdScript = ""
			+ "local maxid = redis.call('incrby',KEYS[1],1);"
			+ "return maxid;";
	/**获取问答的id*/
	public int getFaqId() {
		
		long maxid = redis.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				Long x = connection.eval(faqIdScript.getBytes(),ReturnType.INTEGER,1,FAQ_MAXID.getBytes());
				return x;
			}
		});
		return (int)maxid;
	}
	
	
	public void setFaqListPageCache(List<FAQBean> faqs,int page) {
		this.redis.opsForHash().put(BBS_FAQ_PAGE_LIST,page+"", faqs);
	}
	
	//在设置total的时候加入缓存时间过期设置
	public void setFaqListPageTotalCache(int total) {
		this.redis.opsForHash().put(BBS_FAQ_PAGE_LIST,"total",total);
		this.redis.expire(BBS_FAQ_PAGE_LIST,10,TimeUnit.MINUTES);
	}

	@SuppressWarnings("unchecked")
	public List<FAQBean> getFaqListPage(int page){
		return (List<FAQBean>) this.redis.opsForHash().get(BBS_FAQ_PAGE_LIST, page+"");
	}
	
	public Integer getFaqListPageTotal() {
		return (Integer) this.redis.opsForHash().get(BBS_FAQ_PAGE_LIST,"total");
	}
	
	
	public void deleteFaqListPageCache() {
		this.redis.delete(BBS_FAQ_PAGE_LIST);
	}
}
